﻿/*****************************************介绍*****************************************
 * 作者：24531
 * 创建时间：2025-09-27 12:02:27
 * 功能：界面【CItyPreviewPanel】的View
        1、负责收集所有控件
**************************************************************************************/

using Game.Framework;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.CItyPreviewPanel
{
    public class CItyPreviewPanelView : PopupPanel
    {
        public const string PANEL_KEY = "Game_UI_CItyPreviewPanel_CItyPreviewPanel_Key";

        private CityPanel.Item m_CityItem;
        private PreviewPanel.Item m_Item, m_Item1, m_Cur, m_Next;
        private int m_NeedCoins, m_NeedAdCount;

        protected override void RegistAny()
        {
            PanelManager.Instance.RegistPanel(PANEL_KEY, this);
        }

        protected override void UnregistAny()
        {
            PanelManager.Instance.UnregistPanel(PANEL_KEY);
        }

        protected override void InitUI()
        {
            m_Item = transform.Find("Mask/Item").GetComponent<PreviewPanel.Item>();
            m_Item1 = transform.Find("Mask/Item1").GetComponent<PreviewPanel.Item>();
        }

        public override void Init(object data)
        {
            EventCenter.Instance.AddEventListen<EAdType, ETriggerAdType>(GameEventName.ON_SHOW_SUCCESS_AD, CloseSuccessAd);
            m_CityItem = data as CityPanel.Item;
            m_Item.Init(m_CityItem.Icon);
            m_Cur = m_Item;
            m_Next = m_Item1;
            UpdateBtnStatus();
            UIUtility.TopDisplayLobbyPanelInfo(true, true);
        }

        private void UpdateBtnStatus()
        {
            GetControl<Button>(CItyPreviewPanelNotifications.PreviousBtn_CTRL).gameObject.SetActive(m_CityItem.Previous != null);
            GetControl<Button>(CItyPreviewPanelNotifications.NextBtn_CTRL).gameObject.SetActive(m_CityItem.Next != null);
            var cityData = m_CityItem.CityData;
            var data = m_CityItem.Data;
            m_NeedAdCount = BaseDataMgr.Instance.GetCountryCfg(cityData.countryName)[cityData.name].adCount;
            m_NeedCoins = BaseDataMgr.Instance.GetCountryCfg(cityData.countryName)[cityData.name].coins;
            bool isCanFreeDownload = data.lookAdCount >= m_NeedAdCount;

            GetControl<Button>(CItyPreviewPanelNotifications.CoinBtn_CTRL).gameObject.SetActive(!isCanFreeDownload);
            GetControl<Button>(CItyPreviewPanelNotifications.ClaimBtn_CTRL).gameObject.SetActive(!isCanFreeDownload);
            GetControl<Button>(CItyPreviewPanelNotifications.DownloadBtn_CTRL).gameObject.SetActive(isCanFreeDownload);
            if (!isCanFreeDownload)
            {
                GetControl<Text>(CItyPreviewPanelNotifications.CoinNum_CTRL).text = $"{BaseDataMgr.Instance.GetLanguageValue("Download")}  {m_NeedCoins}";
                GetControl<Text>(CItyPreviewPanelNotifications.ClaimNum_CTRL).text = $"{BaseDataMgr.Instance.GetLanguageValue("Download")}  ( {data.lookAdCount}/{m_NeedAdCount} )";
            }
        }

        private void OnDisable()
        {
            EventCenter.Instance.RemoveEventListen<EAdType, ETriggerAdType>(GameEventName.ON_SHOW_SUCCESS_AD, CloseSuccessAd);
            m_Item.SetPos(Vector2.zero, false);
            m_Item1.SetPos(Vector2.right * -810, false);
            UIUtility.TopDisplayLobbyPanelInfo(false, true);
        }

        protected override void ClickBtnHandle(string name)
        {
            switch (name)
            {
                case CItyPreviewPanelNotifications.CloseBtn_CTRL:
                    UIUtility.HidePanel(PANEL_KEY);
                    break;

                case CItyPreviewPanelNotifications.PreviousBtn_CTRL:
                    m_Next.Init(m_CityItem.Previous.Icon);
                    m_Next.SetPos(Vector2.right * -810, false);
                    m_Cur.SetPos(Vector2.right * 810, true);
                    m_Next.SetPos(Vector2.zero, true);
                    m_Cur = m_Cur == m_Item ? m_Item1 : m_Item;
                    m_Next = m_Next == m_Item1 ? m_Item : m_Item1;
                    m_CityItem = m_CityItem.Previous;
                    UpdateBtnStatus();
                    break;

                case CItyPreviewPanelNotifications.NextBtn_CTRL:
                    m_Next.Init(m_CityItem.Next.Icon);
                    m_Next.SetPos(Vector2.right * 810, false);
                    m_Cur.SetPos(Vector2.right * -810, true);
                    m_Next.SetPos(Vector2.zero, true);
                    m_Cur = m_Cur == m_Item ? m_Item1 : m_Item;
                    m_Next = m_Next == m_Item1 ? m_Item : m_Item1;
                    m_CityItem = m_CityItem.Next;
                    UpdateBtnStatus();
                    break;

                case CItyPreviewPanelNotifications.SetBtn_CTRL:
                    GameDataMgr.Instance.CurHallBgInfo = m_CityItem.Data.assetsInfo;
                    UIUtility.ShowToast(BaseDataMgr.Instance.GetLanguageValue("Successfully set"));
                    break;

                case CItyPreviewPanelNotifications.CoinBtn_CTRL:
                    if (GameDataMgr.Instance.TotalCoinsNum < m_NeedCoins)
                    {
                        UIUtility.ShowToast(BaseDataMgr.Instance.GetLanguageValue("The remaining gold coins are insufficient!!"));
                        break;
                    }
                    m_CityItem.Data.lookAdCount = m_NeedAdCount;
                    DownloadHandle();
                    GameDataMgr.Instance.UpdateCoins(-m_NeedCoins);
                    break;

                case CItyPreviewPanelNotifications.ClaimBtn_CTRL:
                    NativeInteraction.Instance.ShowRewardAd(ETriggerAdType.DownloadImg);
                    break;

                case CItyPreviewPanelNotifications.DownloadBtn_CTRL:
                    DownloadHandle();
                    break;
            }
        }

        private void DownloadHandle()
        {
            string path = AssetCacheServer.Instance.GetAssetPath(m_CityItem.Data.assetsInfo.type, m_CityItem.Data.assetsInfo.id);
            NativeInteraction.Instance.SaveImageToAlbum(path);
            UpdateBtnStatus();
        }

        private void CloseSuccessAd(EAdType adType, ETriggerAdType triggerAdType)
        {
            if (adType != EAdType.RewardedVideo || triggerAdType != ETriggerAdType.DownloadImg) return;
            m_CityItem.Data.lookAdCount++;
            UpdateBtnStatus();
            if (m_CityItem.Data.lookAdCount >= m_NeedAdCount)
                DownloadHandle();
        }
    }
}